// pages/community/xiewen/index.js
var app = getApp();
var shetuan = require('../shetuan.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      pageNum:1,
  xieWenData:[
    ],id:0,
        from:4,
        datainfo:{},
      pingLunData:{isShowPingLun:0,value:'',isshuru:0,yifa:0},
        isShowPingLun:1,
        pingLunContent:'',
        bookImage:['1','1','1'],
        pinglunlists:[],
        liulannum:0
  },
  editor:function(e){
    wx.navigateTo({
      url:'/pages/community/xiewen/editor'
    })
  },
  toInfo:function(e){
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
          url:'/pages/community/xiewen/info?id='+id
      })
  },

    onReachBottom: function (e) {
        // var that = this;
        // // 显示加载图标
        // wx.showLoading({
        //     title: '玩命加载中',
        // })
        // that.setData({
        //     pageNum:1,
        //     xieWenData:[]
        // })
        // this.getDataList();
    },
    onPullDownRefresh:function(e){
        console.log('下拉刷新')
        wx.showLoading({
            title: '玩命加载中',
        })
        var that = this;
        that.setData({
            pageNum:that.data.pageNum+1

        })
        this.getDataList();
    },
    onLoad:function(e){

    },

    onShow:function(){
        this.setData({
            xieWenData:[
            ],
            pageNum:1
        })
        this.getDataList();
    },

    getDataList:function(e){
        var that = this;
        wx.request({
            url:getApp().data.server+'/index.php/Tongcheng/Book/getSheQuList?uid='+getApp().globalData.userInfo.uid+'&type=4&pagenum='+that.data.pageNum,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data))
                wx.hideLoading();
                if(0 != d.data){
                    that.setData({
                        xieWenData:that.data.xieWenData.concat(d.data)
                    })
                }else{
                    wx.showModal({
                        title:'提示',
                        content:'已加载所有'
                    })
                }
            }
        })
    },
    hidePingLun:function(){
        this.setData({
            pingLunData:{isShowPingLun:0}
        })
    },


    showPingLun:function(e){
        var that = this;
        this.setData({
            pingLunData:{isShowPingLun:1,yifa:0},
            pingLunContent:'',
            id:e.currentTarget.dataset.id,
            from:e.currentTarget.dataset.from
        })
    },

    pingluninput:function(e){
        var that = this;
        this.setData({
            pingLunContent:e.detail.value
        })
    },
    fasong:function(e){
        this.setData({
            "pingLunData.yifa":1
        })
        getApp().globalData.iswangchengpinglun=0;
        var that = this;
        var formdata = {
            pingLunContent:this.data.pingLunContent,
            uid:getApp().globalData.userInfo.uid,
            otherid:that.data.id,
            from:that.data.from
        }

        var i = setInterval(function(e){
            // console.log(getApp().globalData.iswangchengpinglun);
            if(1 == getApp().globalData.iswangchengpinglun){
                that.hidePingLun();
                that.setData({
                    xieWenData:[
                    ]
                })
                that.getDataList();
                clearInterval(i);
            }
        },1000)
        // console.log(JSON.stringify(formdata))
        app.pinglun(formdata);

    },//点赞
    dianzan:function(e){
        var that = this;
        var from = e.currentTarget.dataset.from;
        var id = e.currentTarget.dataset.id;
        var formdata = {
            otherid:id,
            from:from
        }
        app.dianZan(formdata);
        //修改当前数据源，避免重新请求数据
        //3社区的书评 4社区的话题 5社区的问答 6社区的写文
        var temp = that.data.xieWenData;
        console.log(temp);
        for(var i=0;i<temp.length;i++){
            var item = temp[i];
            if(id == item.id){
                if(1 == item.isDianZan){
                    item.dianzan_num=--item.dianzan_num;
                    item.isDianZan=0;
                }else if(0 == item.isDianZan){
                    item.dianzan_num=++item.dianzan_num;
                    item.isDianZan=1;
                }
            }
        }
        that.setData({
            xieWenData:temp
        })
    },
    onShareAppMessage: function (res) {
        // if (res.from === 'button') {
        //   // 来自页面内转发按钮
        //   console.log(res.target)
        // }
        var id = res.target.dataset.id;
        var path = '/pages/community/xiewen/info?id='+id;
        return {
            title: '分享',
            path: path
        }
    },
    bindfocus:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":1
        })
    },
    bindblur:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":0
        })
    }
})